<?php

namespace App\Http\Controllers\home;

use Illuminate\Http\Request;

use App\Http\Requests;

use Intervention\Image\ImageManagerStatic as Image;

use App\Http\Controllers\Controller;

use DB;
class passController extends Controller
{
    //个人信息页面
    public function index()
    {
        // 查找用户信息表且名字为当前登录的用户名
        $list = DB::table('storeuser')
            ->where('userName','=',session('user')->userName)
            ->get();
        return view('home.password',['list'=>$list]);
    }

    public function edit($id)
    {
        $value = DB::table('storeuser')->where('userId',$id)->first();
        return view('home.password',['v'=>$value]);
    }

    public function update($id,Request $request)
    {

        // 自定义错误信息
        $message = [
            'required' => ':attribute 字段值是必填项',
            'between' => ':attribute 必须介于 :min - :max。',
            'same' => '密码不一致',
        ];
        //设定验证规则
        $this->validate($request, [
            'oldPass' => 'required',
            'userPass' => 'required|between:6,12',
            'rePass' => 'required|same:userPass',
        ],$message);

        $pw = DB::table('storeuser')->select('userPass')->where('userId',$id)->first();

        $data = $request->except('_token','_method');

        if ($pw->userPass != md5($data['oldPass'])) {
            return redirect('/pass')->with('msg','修改失败');
        }
        
        $data['userPass'] = md5($data['userPass']);

        // 去除不需要的字段 
        $data = $request->except('_token','_method','rePass','oldPass');

        $data['userPass'] = md5($data['userPass']);
        
        $row = DB::table('storeuser')->where('userId',$id)->update($data);

        if($row>0){
            return redirect('/userLogin')->with('msg','修改成功');
        }
    }
}
